/**
 * 超级管理员控制器
 *
 * 开发人员：徐少洋
 * 开发时间：2025-11-21
 * 模块职责：提供超级管理员的认证和管理REST API接口
 */
package com.pbl.controller;

import com.pbl.common.model.ApiResponse;
import com.pbl.entity.SuperAdmin;
import com.pbl.entity.SuperAdminLog;
import com.pbl.service.SuperAdminService;
import lombok.Data;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/super-admin")
public class SuperAdminController {
    
    private final SuperAdminService superAdminService;
    
    public SuperAdminController(SuperAdminService superAdminService) {
        this.superAdminService = superAdminService;
    }
    
    /**
     * 超级管理员登录
     */
    @PostMapping("/login")
    public ApiResponse<Map<String, Object>> login(@Valid @RequestBody LoginRequest request) {
        return ApiResponse.success(superAdminService.login(request.getUsername(), request.getPassword()));
    }
    
    /**
     * 获取当前超级管理员信息
     */
    @GetMapping("/me")
    public ApiResponse<SuperAdmin> getCurrentSuperAdmin(@RequestAttribute("superAdminId") Long superAdminId) {
        return ApiResponse.success(superAdminService.get(superAdminId));
    }
    
    /**
     * 获取所有超级管理员列表
     */
    @GetMapping
    public ApiResponse<List<SuperAdmin>> list(@RequestAttribute("superAdminId") Long superAdminId) {
        return ApiResponse.success(superAdminService.list());
    }
    
    /**
     * 创建新的超级管理员
     */
    @PostMapping
    public ApiResponse<SuperAdmin> create(@RequestAttribute("superAdminId") Long currentSuperAdminId,
                                         @Valid @RequestBody CreateSuperAdminRequest request,
                                         HttpServletRequest httpRequest) {
        SuperAdmin superAdmin = SuperAdmin.builder()
            .username(request.getUsername())
            .email(request.getEmail())
            .phone(request.getPhone())
            .realName(request.getRealName())
            .build();
        
        SuperAdmin created = superAdminService.create(superAdmin, request.getPassword());
        
        // 记录日志
        superAdminService.logAction(currentSuperAdminId, "CREATE_SUPER_ADMIN", "super_admin", 
            created.getId(), "创建超级管理员: " + created.getUsername(),
            httpRequest.getRemoteAddr(), httpRequest.getHeader("User-Agent"));
        
        return ApiResponse.success(created);
    }
    
    /**
     * 更新超级管理员信息
     */
    @PutMapping("/{id}")
    public ApiResponse<SuperAdmin> update(@RequestAttribute("superAdminId") Long currentSuperAdminId,
                                         @PathVariable Long id,
                                         @Valid @RequestBody UpdateSuperAdminRequest request,
                                         HttpServletRequest httpRequest) {
        SuperAdmin superAdmin = new SuperAdmin();
        superAdmin.setEmail(request.getEmail());
        superAdmin.setPhone(request.getPhone());
        superAdmin.setRealName(request.getRealName());
        superAdmin.setStatus(request.getStatus());
        
        SuperAdmin updated = superAdminService.update(id, superAdmin);
        
        // 记录日志
        superAdminService.logAction(currentSuperAdminId, "UPDATE_SUPER_ADMIN", "super_admin", 
            id, "更新超级管理员信息: " + updated.getUsername(),
            httpRequest.getRemoteAddr(), httpRequest.getHeader("User-Agent"));
        
        return ApiResponse.success(updated);
    }
    
    /**
     * 获取操作日志
     */
    @GetMapping("/logs")
    public ApiResponse<Page<SuperAdminLog>> getLogs(@RequestAttribute("superAdminId") Long superAdminId,
                                                    @RequestParam(defaultValue = "0") int page,
                                                    @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.success(superAdminService.getLogs(superAdminId, page, size));
    }
    
    /**
     * 获取针对特定目标的操作日志
     */
    @GetMapping("/logs/{targetType}/{targetId}")
    public ApiResponse<List<SuperAdminLog>> getTargetLogs(@PathVariable String targetType,
                                                          @PathVariable Long targetId) {
        return ApiResponse.success(superAdminService.getTargetLogs(targetType, targetId));
    }
    
    // DTO类
    @Data
    public static class LoginRequest {
        @NotBlank
        private String username;
        
        @NotBlank
        private String password;
    }
    
    @Data
    public static class CreateSuperAdminRequest {
        @NotBlank
        private String username;
        
        @NotBlank
        private String email;
        
        private String phone;
        
        private String realName;
        
        @NotBlank
        private String password;
    }
    
    @Data
    public static class UpdateSuperAdminRequest {
        private String email;
        private String phone;
        private String realName;
        private Integer status;
    }
}

